package com.yupi.mianshiya.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.mianshiya.common.BaseResponse;
import com.yupi.mianshiya.common.ErrorCode;
import com.yupi.mianshiya.common.ResultUtils;
import com.yupi.mianshiya.exception.ThrowUtils;
import com.yupi.mianshiya.model.entity.Note;
import com.yupi.mianshiya.service.NoteService;
import com.yupi.mianshiya.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "笔记接口")
@RestController
@RequestMapping("/note")
@Slf4j
public class NoteController {
    @Resource
    private NoteService noteService;

    @Resource
    private UserService userService;

    @ApiOperation("获取个人笔记列表")
    @GetMapping("/get")
    public BaseResponse<Note> getNote(Note note, HttpServletRequest request) {


        ThrowUtils.throwIf(note.getQuestionId() == null, ErrorCode.PARAMS_ERROR);

        Long userId = userService.getLoginUser(request).getId();
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("questionId", note.getQuestionId());
        Note noteResult = noteService.getOne(queryWrapper);


        if (noteResult == null) {
            return ResultUtils.success(null);
        }
        //        不是本人就报错
        ThrowUtils.throwIf(!noteResult.getUserId().equals(userId), ErrorCode.NO_AUTH_ERROR);
        return ResultUtils.success(noteResult);
    }

    @ApiOperation("添加笔记")
    @PostMapping("/add")
    public BaseResponse<Boolean>  addNote(@RequestBody Note note, HttpServletRequest request) {
        ThrowUtils.throwIf(note.getQuestionId() == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(note.getContent() == null, ErrorCode.PARAMS_ERROR);
        Long userId = userService.getLoginUser(request).getId();
        note.setUserId(userId);
        //先查有没有笔记
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("questionId", note.getQuestionId());
        Note noteResult = noteService.getOne(queryWrapper);

        ThrowUtils.throwIf(noteResult!= null, ErrorCode.FORBIDDEN_ERROR, "不能重复添加笔记");



        boolean save = noteService.save(note);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @ApiOperation("修改笔记")
    @PostMapping("/update")
    public BaseResponse<Boolean>  updateNote(@RequestBody Note note, HttpServletRequest request) {
        ThrowUtils.throwIf(note.getId() == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(note.getContent() == null, ErrorCode.PARAMS_ERROR);
        Long userId = userService.getLoginUser(request).getId();
        note.setUserId(userId);
        //先查有没有笔记
        Note noteResult = noteService.getById(note.getId());
        ThrowUtils.throwIf(noteResult == null, ErrorCode.NOT_FOUND_ERROR,"笔记不存在");
//        不是本人就报错
        ThrowUtils.throwIf(!noteResult.getUserId().equals(userId), ErrorCode.NO_AUTH_ERROR);

        boolean update = noteService.updateById(note);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @ApiOperation("删除笔记")
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteNote(Note note, HttpServletRequest request) {
        ThrowUtils.throwIf(note.getId() == null, ErrorCode.PARAMS_ERROR);
        Long userId = userService.getLoginUser(request).getId();
        //先查有没有笔记
        Note noteResult = noteService.getById(note.getId());
        ThrowUtils.throwIf(noteResult == null,  ErrorCode.NOT_FOUND_ERROR,"笔记不存在");
//        不是本人就报错
        ThrowUtils.throwIf(!noteResult.getUserId().equals(userId), ErrorCode.NO_AUTH_ERROR);
        boolean remove = noteService.removeById(note.getId());
        ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }



}
